package cn.edu.neusoft.zengweidong707.neufood.fragment;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.zengweidong707.neufood.R;
import cn.edu.neusoft.zengweidong707.neufood.adapter.FoodListAdapter;
import cn.edu.neusoft.zengweidong707.neufood.bean.FoodListBean;
import cn.edu.neusoft.zengweidong707.neufood.listener.ListListener;
import cn.edu.neusoft.zengweidong707.neufood.model.UserModel;


public class SearchFragment extends BaseFragment {
    private UserModel model;
    private EditText et_search;
    private Button btn_search;
    private RecyclerView recyclerView;
    private List<FoodListBean> list;
    private ListListener listListener;
    private FoodListAdapter foodListAdapter;
    private LinearLayoutManager linearLayoutManager;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setLayout_file(R.layout.fragment_search);
        listListener = new ListListener<FoodListBean>() {
            @Override
            public void onResponse(List<FoodListBean> foodListBeans) {
                list = foodListBeans;
                if(!list.isEmpty()) {
                    recyclerView.setAdapter(new FoodListAdapter(getActivity(), list));
                } else {
                    Toast.makeText(getActivity(), "没有找到对应菜谱", Toast.LENGTH_SHORT).show();
                    recyclerView.setAdapter(new FoodListAdapter(getActivity(), list));
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            }
        };
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    void initView() {
        et_search = (EditText) view.findViewById(R.id.et_search);
        btn_search = (Button) view.findViewById(R.id.btn_search);
        recyclerView = (RecyclerView) view.findViewById(R.id.search_list);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

    }

    @Override
    void initEvent() {
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = et_search.getText().toString();
                if(!"".equals(s)) {
                    model = new UserModel();
                    model.getSearch(s, listListener);
                } else {
                    Toast.makeText(getActivity(),"请输入要搜索的菜谱信息！",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    @Override
    void initData() {

    }

    public SearchFragment() {
        // Required empty public constructor
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
